package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MD5Util;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.dbutils.SpUtil;
import com.kpx.app.utils.encrypt.Toolkit;
import com.kpx.app.utils.uiutils.IntentUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;
import com.kpx.app.wigdet.ClearEditText;
import com.tencent.bugly.crashreport.biz.UserInfoBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by congdy on 17/3/1.
 *  注册
 */

public class ActivityRegister extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.cet_phone_num)
    ClearEditText cetPhoneNum;
    @BindView(R.id.cet_code)
    ClearEditText cetCode;
    @BindView(R.id.btn_code)
    Button btnCode;
    @BindView(R.id.cet_pwd)
    ClearEditText cetPwd;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.txt_licener)
    TextView txtLicener;
    private int time = 120;
    @BindView(R.id.img_3)
    ImageView img_3;
    @BindView(R.id.cb)
    CheckBox cb;
    private boolean isShowpwd = true;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        UIUtils.setTitle(this, R.color.white, R.color.black_0a, "注册");
        titleview.setBackClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtil.gotoActivityAndFinish(ActivityRegister.this,ActivityLogin.class);
            }
        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            time--;
            if (time <= 0) {
                btnCode.setEnabled(true);
                btnCode.setText("获取验证码");
            } else {
                btnCode.setText(time + "s");
                handler.sendEmptyMessageDelayed(0, 1000);
            }
        }
    };

    @Override
    public void initData() {

    }
    @Override
    protected void onResume() {
        super.onResume();
        long lasttime = SpUtil.getLong(Constants.TIME_CODE_1);
        if(lasttime!=0&&System.currentTimeMillis()-lasttime<120*1000){
            btnCode.setEnabled(false);
            time = (int)((120*1000-System.currentTimeMillis()+lasttime)/1000);
            handler.sendEmptyMessage(0);
        }
    }
    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    @OnClick({R.id.btn_code, R.id.btn_register, R.id.txt_licener, R.id.img_3})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_code:
                if (TextUtils.isEmpty(cetPhoneNum.getText().toString().trim())) {
                    ToastUtil.getInstance().showToast(ActivityRegister.this, "请输入您的手机号码");
                    return;
                }
                showLoadingDialog();
                UserInfoUtils.getCode(ActivityRegister.this, cetPhoneNum.getText().toString().trim(), 1, new UserInfoUtils.OnGetCodeBackListener() {
                    @Override
                    public void back(boolean s) {
                        dissmissLoadingDialog();
                        if (s) {
                            time = 120;
                            btnCode.setEnabled(false);
                            handler.sendEmptyMessage(0);
                            ToastUtil.getInstance().showToast(ActivityRegister.this, "获取成功");
                            SpUtil.putLong(Constants.TIME_CODE_1,System.currentTimeMillis());
                        }
                    }
                });
                break;
            case R.id.btn_register:
                if (TextUtils.isEmpty(cetPhoneNum.getText().toString().trim())) {
                    ToastUtil.getInstance().showToast(ActivityRegister.this, "请输入您的手机号码");
                    return;
                }
                if (TextUtils.isEmpty(cetCode.getText().toString().trim())) {
                    ToastUtil.getInstance().showToast(ActivityRegister.this, "请输入您的验证码");
                    return;
                }
                if (TextUtils.isEmpty(cetPwd.getText().toString().trim())) {
                    ToastUtil.getInstance().showToast(ActivityRegister.this, "请输入您的密码");
                    return;
                }
                if (!cb.isChecked()) {
                    ToastUtil.getInstance().showToast(ActivityRegister.this, "请同意用户协议");
                    return;
                }
                toRegister();
                break;
            case R.id.txt_licener:
                ActivityWebShow.type = 0;
                ActivityWebShow.url = "http://myshowbusiness.nullwei.com/shareRegister/user_pact.html";
                IntentUtil.gotoActivity(this, ActivityWebShow.class);
                break;
            case R.id.img_3:
                if (isShowpwd) {
                    isShowpwd = false;
                    img_3.setBackgroundResource(R.drawable.login_right_see);
                    cetPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
//                    cetPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    isShowpwd = true;
                    img_3.setBackgroundResource(R.drawable.see_pwd_icon);
                    cetPwd.setInputType(InputType.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_NORMAL);
//                    cetPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());

                }
                break;
        }
    }

    private void toRegister() {
        MyHashMap map = new MyHashMap();
        map.put("phone", cetPhoneNum.getText().toString().trim());
        map.put("pwd", MD5Util.encrypt(cetPwd.getText().toString().trim()));
        map.put("authCode", cetCode.getText().toString().trim());
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                if (result.getCode() == 0) {
                    SpUtil.putString(Constants.KEY_USER_PHONE, cetPhoneNum.getText().toString().trim());
                    SpUtil.putString(Constants.KEY_USER_PWD, cetPwd.getText().toString().trim());
                    ToastUtil.getInstance().showToast(ActivityRegister.this, "注册成功");
                    IntentUtil.gotoActivityAndFinish(ActivityRegister.this,ActivityLogin.class);
                } else {
                    ToastUtil.getInstance().showToast(ActivityRegister.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                ToastUtil.getInstance().showToast(ActivityRegister.this, error);
            }
        }, UserInfoBean.class, HttpDataRequest.TYPE_OBJECT).POST(HttpDataReqUrl.URL_POST_REGISTER, true, map);
    }

    @Override
    public void onBackPressed() {
        IntentUtil.gotoActivityAndFinish(ActivityRegister.this,ActivityLogin.class);
    }
}
